<template>
  <div class="app-container">
    <jy-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-row :gutter="10" class="mb8 cd-pr8 top-right-btn small-line-height">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
            @click="handleDelete">删除</el-button>
        </el-col>
      </el-row>
      <el-table border stripe v-loading="loading" :data="form.detailList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="清单编号" align="center" prop="manifestCode" min-width="120" show-overflow-tooltip />
        <el-table-column label="清单名称" align="center" prop="manifestName" min-width="120" show-overflow-tooltip />
        <el-table-column label="型号" align="center" prop="model" min-width="100" show-overflow-tooltip />
        <el-table-column label="规格" align="center" prop="specification" min-width="80" show-overflow-tooltip />
        <el-table-column label="单位" align="center" prop="unit" min-width="80" show-overflow-tooltip />
        <el-table-column label="累计采购数量" align="center" prop="totalPurchaseNumber" min-width="120"
          show-overflow-tooltip />
        <el-table-column label="本月采购数量" align="center" prop="monthPurchaseNumber" min-width="120">
          <template slot-scope="scope">
            <el-input size="mini" type="number" v-model="scope.row.monthPurchaseNumber" />
          </template>
        </el-table-column>
        <el-table-column label="供货地点" align="center" prop="bysqgyslxhd" min-width="120">
          <template slot-scope="scope">
            <el-input size="mini" v-model="scope.row.bysqgyslxhd" />
          </template>
        </el-table-column>
        <el-table-column label="联系人" align="center" prop="personnel" min-width="120">
          <template slot-scope="scope">
            <el-input size="mini" v-model="scope.row.personnel" />
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center" prop="phone" min-width="130">
          <template slot-scope="scope">
            <el-input size="mini" v-model="scope.row.phone" />
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </jy-dialog>
    <manifest-select ref="manifestSelect" @callBack="setDetailList" />
  </div>
</template>

<script>
import { getMaterialPlanDetails, updateMaterialPlanDetails } from "@/api/ma/materialPlan";
import manifestSelect from '../materialManifest/manifestSelect.vue';
export default {
  components: { manifestSelect },
  name: "MaterialPlanDetails",
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 材料计划详情表格数据
      materialPlanDetailsList: [],
      // 弹出层标题
      title: "编辑明细",
      // 是否显示弹出层
      open: false,
      // 查询参数
      planId: undefined,
      // 表单参数
      form: {
        detailList: [],
        infoData: {}
      },
      // 表单校验
      rules: {
      }
    };
  },
  created () {
  },
  methods: {
    init (planId) {
      this.planId = planId;
      this.open = true;
      this.getMaterialPlanDetails();
    },
    /** 查询材料计划详情列表 */
    getMaterialPlanDetails () {
      this.loading = true;
      getMaterialPlanDetails(this.planId).then(response => {
        this.form = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    reset () {
      this.form = {
        detailList: [],
        infoData: {}
      };
    },
    // 表单重置
    setInfo (item) {
      const info = {
        planDetailsId: undefined,
        planId: this.form.infoData.planId,
        bdId: this.form.infoData.bdId,
        zdId: this.form.infoData.zdId,
        gysId: this.form.infoData.gysId,
        type: undefined,
        manifestId: item.manifestId,
        manifestCode: item.manifestCode,
        manifestName: item.manifestName,
        model: item.model,
        specification: item.specification,
        unit: item.unit,
        monthPurchaseNumber: undefined,
        totalPurchaseNumber: undefined,
        personnel: undefined,
        phone: undefined,
        bysqgyslxhd: undefined,
      };
      return info;
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.manifestId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.$refs.manifestSelect.init(this.form.infoData.gysId, this.form.detailList);
    },
    setDetailList (list) {
      list.forEach(item => {
        this.form.detailList.push(this.setInfo(item));
      });
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
    },
    /** 提交按钮 */
    submitForm () {
      this.buttonLoading = true;
      updateMaterialPlanDetails(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.$emit("callBack");
        this.reset();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete () {
      this.ids.forEach(item => {
        const index = this.form.detailList.findIndex(e => e.manifestId);
        this.form.detailList.splice(index, 1);
      });
    },
  }
};
</script>
